﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CarRental.View.Order {
    public partial class ReviewOrder : System.Web.UI.Page {
        DbAccess _dbAccess = new DbAccess();
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {

                ddlOrderStatus.DataSource = new CarRental.Helpers.OptionItems.OrderStatusLst(true);
                ddlOrderStatus.DataValueField = "Key";
                ddlOrderStatus.DataTextField = "Title";
                ddlOrderStatus.DataBind();
            }

        }

        protected void gv_OrderList_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "Edit") {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gv_OrderList.Rows[index];
                //int ServerID = Convert.ToInt32(gvUser.DataKeys[index].Value);
                var orderId = row.Cells[1].Text;

                Response.Redirect("~/View/Order/EditOrder.aspx?OrderId=" + orderId);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e) {
            string _OrderNo = "";
            string _OrderStatus = "";
            _OrderNo = tbOrderNo.Text;
            _OrderStatus = ddlOrderStatus.Text;

            if (Shared.LoginInfo.UserType == "ADMIN" || Shared.LoginInfo.UserType == "STAFF") {
                List<order_list> _orderLst = _dbAccess.GetOrderLst().Where(w => (w.order_no == _OrderNo || _OrderNo == "" &&
                                                                                 (w.status == _OrderStatus || _OrderStatus == ""))).ToList<order_list>();
                gv_OrderList.DataSource = _orderLst;
                gv_OrderList.DataBind();
            } else if (Shared.LoginInfo.UserType == "CUSTOMER") {
                List<order_list> _orderLst = _dbAccess.GetOrderLst().Where(w => w.customer_id == Shared.LoginInfo.LoginName &&
                                                                          (w.order_no == _OrderNo || _OrderNo == "") &&
                                                                           (w.status == _OrderStatus || _OrderStatus == "")).ToList<order_list>();
                gv_OrderList.DataSource = _orderLst;
                gv_OrderList.DataBind();
                if (_OrderStatus == "C") {
                    gv_OrderList.Columns[0].Visible = false;
                }

            }
        }
    }
}